//
//  MHMainFrameItemViewModel.m
// MiaoCircle
//
//Created by acgera on 2018/4/9.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHHomeDiscoverItemViewModel.h"
#import "MHDiscoverHomeModel.h"
#import "MHMomentVideo.h"
#import "NSMutableAttributedString+MHMoment.h"
@interface MHHomeDiscoverItemViewModel ()
/// LiveRoom
//@property (nonatomic, readwrite, strong) MHDiscoverHomeModel *homeDiscover;
/// cellHeight
//@property (nonatomic, readwrite, assign) CGSize cellSize;

@end


@implementation MHHomeDiscoverItemViewModel

- (instancetype)initWithCircle:(MHDiscoverHomeModel *)homeDiscover{
    if (self = [super init]) {
        self.homeDiscover = homeDiscover;
        if(self.cellSize.width == 0)
        {
            CGFloat scale;
            if (homeDiscover.vedioObj.videoURL) {
                scale = 1;
            }else
            {
                if (homeDiscover.attm) {
                    scale =  homeDiscover.attm.width / ((homeDiscover.attm.height == 0) ? 0.01 : homeDiscover.attm.height);
                }else
                {
                    scale =  1;
                }
            }
            
            CGFloat imageWith = (MH_SCREEN_WIDTH - 20) / 2;
            
            scale = (scale == 0) ? 0.01 : scale;
            CGFloat imageHeight = imageWith / scale;
            if (imageHeight > 230) {
                imageHeight = 230;
            }
            self.heightConstraint = imageHeight;
            imageHeight += 50; //底部内容
            
            BOOL isAsk = MHStringIsNotEmpty(self.homeDiscover.askedUName);
            NSString *content = self.homeDiscover.content;
            NSString *askString = [NSString stringWithFormat:@"向%@提问\n", self.homeDiscover.askedUName];
            if (isAsk) {
                content = [NSString stringWithFormat:@"%@%@",askString,content];
            }
            
            CGFloat contentHeight = [self getHeightWithContent:content];
            CGFloat disoverHeight = imageHeight + (contentHeight >= 36 ? 36 :contentHeight);
            self.cellSize = CGSizeMake(imageWith, disoverHeight);
        }
    }
    return self;
}


- (CGFloat)getHeightWithContent:(NSString *)content
{
    // 计算文本高度
    UIFont *fnt = MHMediumFont(13); // 文字的size
    CGFloat imageWith = (MH_SCREEN_WIDTH - 20) / 2 - 20;
    CGRect tmpRect = [content boundingRectWithSize:CGSizeMake(imageWith, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil] context:nil];// [UIScreen mainScreen].bounds.size.width - 40 设置最大显示的宽度
    CGFloat textheight = tmpRect.size.height;
    
    return textheight - 5; // 文本的高度
}



-(YYTextLayout *)tileTextLayout
{
    if (!_tileTextLayout) {
        YYTextLayout *nameLableLayout;
//        if(MHStringIsNotEmpty(self.homeDiscover.content))
//        {
//            NSMutableAttributedString *textAttr = [[NSMutableAttributedString alloc] initWithString:self.homeDiscover.content];
//            textAttr.yy_font = MHMediumFont(13);
//            textAttr.yy_color = MHMainTextColor;
//            textAttr.yy_lineBreakMode = NSLineBreakByCharWrapping;
//            textAttr.yy_alignment = NSTextAlignmentLeft;
//            NSMutableAttributedString *askedAttr;
//            BOOL isAsk = MHStringIsNotEmpty(self.homeDiscover.askedUName);
//            NSString *askString = [NSString stringWithFormat:@"向%@提问\n", self.homeDiscover.askedUName];
//            if (isAsk) {
//                askedAttr = [[NSMutableAttributedString alloc] initWithString:askString];
//                askedAttr.yy_color = MHAlertTextColor;
//                askedAttr.yy_font = MHRegularFont_13;
//                askedAttr.yy_alignment = NSTextAlignmentLeft;
//                [textAttr insertAttributedString:askedAttr atIndex:0];
//            }
//            [textAttr mh_regexContentWithWithEmojiImageFontSize:13];
//            CGFloat imageWith = (MH_SCREEN_WIDTH - 20) / 2;
//            YYTextContainer *nameLableContainer = [YYTextContainer containerWithSize:CGSizeMake(imageWith -20, MAXFLOAT)];
//            nameLableContainer.maximumNumberOfRows = 2;
//            nameLableLayout = [YYTextLayout layoutWithContainer:nameLableContainer text:textAttr];
//            _tileTextLayout = nameLableLayout;
//        }
    }
    return _tileTextLayout;
   
}

//根据高度度求宽度  text 计算的内容  Height 计算的高度 font字体大小
@end
